<?php

include_once($ROOT . "base.inc.php");

class PageController extends BaseController {
  var $song;
  var $allSongs;

  function PageController() {
	parent::__construct();

	$this->title="Songs";
	$this->section="songs";

	$submitted = $this->getParameter("submitted");
	$action = $this->getParameter("action");
	$this->song = new Song();


	$this->allSongs = SongLogic::findAll();

	switch (strtoupper($action))
	{
		case "SAVE" :
			  $this->parseForm();
			  if (!$this->validate()) return;

			  if (!SongLogic::save($this->song))
			  {
			  	 $this->addError("Could not save song");
			  	 break;
			  }

			  $this->addMessage("Saved");
			  $this->allSongs = SongLogic::findAll();
			  break;

		case "LOAD" :
			  $id = $this->getParameter("id");
	  		  $this->song = SongLogic::load($id);
	  		  $this->addMessage("Loaded $id");
	  		  break;
	  	case "CANCEL" :
	  		  HtmlUtil::redirectToPage("searchSongs");
	  	default:
	  		if ($submitted == "1")
	  			$this->addError("File Size Too Large.");
	  			return;

	}

  }

  function parseForm() {
  	$this->song->active = "F";
	$this->song->readForm();

	$audio = SongLogic::getUploadedSong("audioFile");
	if (strlen($audio) > 0)
	{
		$this->song->audio = $audio;
	}
  }

  function validate() {
  	if ($this->song->title == "")
  		$this->addError("A title is required");
  	if ($this->song->author == "")
  		$this->addError("An author is required");
	return (sizeof($this->errs) == 0);
  }

}



?>
